﻿using System;
using DevExpress.XtraEditors.DXErrorProvider;
using PMS.Resources;
using PMS.BLL;
using PMS.Entity;
using PMS.Utils;
using System.Linq;

namespace PMS.ValidationRules
{
    class DateValidateRule: ConditionValidationRule
    {

        private bool CheckKy = false;
        private bool Require = false;
        public DateValidateRule(bool require, bool checkKy)
        {
            Require = require;
            CheckKy = checkKy;
        }

        DongMoKyBO Service = new DongMoKyBO();
        public override bool Validate(System.Windows.Forms.Control control, object value)
        {
            try
            {
                
                DateTime date = DataConvertUltils.ToDateTime(value);
                if (value == null)
                {
                    if (Require)
                    {
                        ErrorText = Messages.GetString(Constants.REQUIRE_MESSAGE);
                        return false;
                    }
                }
                else
                {
                    string maky = date.Year+"-"+((date.Month>9)?date.Month.ToString():"0"+date.Month);
                    DongMoKy ky = Service.GetDongMoKyByID(maky);
                    if (ky == null)
                    {
                        ErrorText = string.Format(Messages.GetString("KyChuMoHoacKhongTonTai"),date.Month,date.Year);
                        return false;
                    }
                    else
                    {
                        if (ky.DongKy)
                        {
                            ErrorText = string.Format(Messages.GetString("KyDaDong"), date.Month, date.Year);
                            return false;
                        }
                    }
                }
                    

                return true;
            }
            catch
            {
                return true;
                //throw new Exception(ex.Message);
            }
        }
    }
}
